package com.linzm.leetcode.primary.exercises1_20230114;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Author zimingl
 * @Date 2023/1/15 13:29
 * @Description: 剑指 Offer 05. 替换空格
 */
public class Demo14 {
    public static void main(String[] args) {
        String s = "We are happy.";
        Demo14 demo14 = new Demo14();
        String resStr = demo14.replaceSpace(s);
        System.out.println(resStr);
    }

    private String replaceSpace(String s) {
        Deque<String> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            stack.push(""+s.charAt(i));
            if (s.charAt(i) == ' ') {
                stack.pop();
                stack.push("%20");
            }
        }
        String resString = "";
        while (!stack.isEmpty()) {
            resString = stack.pop() + resString;
        }
        return resString;
    }

    private String replaceSpace2(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' ') {
                sb.append("%20");
            } else {
                sb.append(s.charAt(i));
            }
        }
        return sb.toString();
    }
}
